@echo off
chcp 65001 >nul

REM Frontend service startup script - Fixed version

echo =========================================
echo Starting Frontend Service
echo =========================================

REM Change to frontend directory
cd /d "%~dp0frontend"

echo Current directory: %cd%
echo.

REM Check if npm is installed
where npm >nul 2>nul
if %errorlevel% neq 0 (
    echo Error: npm command not found
    echo Please ensure Node.js and npm are installed
    pause
    exit /b 1
)

echo npm environment: OK
echo.

REM Check if node_modules exists, if not install dependencies
if not exist "node_modules" (
    echo Installing npm dependencies...
    npm install
    if %errorlevel% neq 0 (
        echo Error: npm dependencies installation failed
        pause
        exit /b 1
    )
) else (
    echo Dependencies already installed, skipping npm install
)

echo.
echo =========================================
echo Starting Vite Development Server...
echo Frontend will be available at: http://localhost:5173
echo Press Ctrl+C to stop the service
echo =========================================
echo.

REM Start Vite development server
npm run dev

REM If we reach here, the service has stopped
echo.
echo =========================================
echo Frontend service has stopped
echo =========================================
pause